/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package snippet.lab4;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hoang.pt
 */
public class StudentDemo {

    public StudentDemo() {
    }

    /**
     * 
     * @param args
     */
    public static void main(String[] args) {

        //display menu
        displayMenu();
    }

    /**
     * 
     */
    public static void displayMenu() {

        //init choice
        int choice;
        Scanner sc = new Scanner(System.in);

        //loop until choice = 3
        do {
            //display menu
            System.out.println("Hanoi-Amsterdam");
            System.out.println("1. Input name");
            System.out.println("2. Display");
            System.out.println("3. Exit");
            System.out.println("------------------");
            System.out.println("Enter your choice: ");

            //read choice
            choice = sc.nextInt();

            //switch choice
            switch (choice) {
                case 1:
                    inputStudent();
                    break;
                case 2:
                    displayAll();
                    break;
                case 3:
                    System.out.println("Bye bye");
                    System.exit(0);
                default:
                    System.out.println("Invalid choice. Enter again.");
                    break;

            }
        } while (choice != 3);

    }

    /**
     * input
     * @author hoang.pt
     */
    public static void inputStudent() {
        System.out.println("Input");

        //init
        Scanner input = new Scanner(System.in);
        Student student = new Student();

        //input all information
        System.out.println("Enter your name:");
        student.setName(input.next());
        System.out.println("Enter your class:");
        student.setClasx(input.next());
        System.out.println("Enter your math:");
        student.setMaths(input.nextInt());
        System.out.println("Enter your physics:");
        student.setPhysics(input.nextInt());
        System.out.println("Enter your chemistry:");
        student.setChemistry(input.nextInt());

        System.out.println(student.toString());

        try {
            //init writer
            FileWriter fWriter = new FileWriter("student.db", true);
            BufferedWriter bWriter = new BufferedWriter(fWriter);

            //write information into file student.db
            bWriter.write(student.getName());
            bWriter.write("\t");
            bWriter.write(student.getClasx());
            bWriter.write("\t");
            bWriter.write(Integer.toString(student.getMaths()));
            bWriter.write("\t");
            bWriter.write(Integer.toString(student.getPhysics()));
            bWriter.write("\t");
            bWriter.write(Integer.toString(student.getChemistry()));
            bWriter.write("\t");
            bWriter.newLine();

            //close
            bWriter.flush();
            bWriter.close();
            fWriter.close();

        } catch (IOException ex) {
        }

    }

    /**
     * display
     * @author hoang.pt
     */
    public static void displayAll() {
        System.out.println("display");
        try {
            //init reader
            FileReader fReader = new FileReader("student.db");
            BufferedReader bReader = new BufferedReader(fReader);

            String line;

            //check line === null, loop
            while ((line = bReader.readLine()) != null) {
                System.out.println(line);
            }

            //close file
            bReader.close();
            fReader.close();

        } catch (FileNotFoundException ex) {
        } catch (IOException e) {
        }

    }
}


